<?php
	class News extends My_Controller{
		function News(){
			parent::My_Controller();
			$this->load->database();
		}
		function index(){
			$pagesize=15;
			$offset = $this->uri->segment(3);
        	$data['data']=$this->db->order_by('create_time','desc')->limit($pagesize,$offset)->get('news')->result_array();
        	$recordcount=$this->db->select('count(*) as recordcount')->get('news')->row()->recordcount;
        	$config['base_url'] = site_url('news');
			$config['total_rows'] = $recordcount;
			$config['per_page'] = $pagesize;
			$this->load->library('pagination'); 
			$this->pagination->initialize($config); 
			$data['pager']=$this->pagination->create_links();
			$this->load->view('admin/news',$data);
		}
		function edit(){
			if($_SERVER["REQUEST_METHOD"]=="GET"){
				$id = floor(floatval($this->uri->segment(4)));
				if(!$id){
					$this->index();
					return;
				}
				$data['data']=$this->db->where('id',$id)->get('news')->row_array();
				if(count($data['data'])==0){
					$this->index();
					return;
				}
				$this->load->view('admin/news_edit',$data);
				return;
			}
			else{
				$id=floor(floatval($this->input->post('id')));
				$data = array(
					'title'=>trim(strip_tags($this->input->post('title'))),
		            'content'=>trim($this->input->post('content'))
     			);
     			$return=$this->db->where('id',$id)->update('news',$data);
     			if($return){
     				$data['data']=array(
						'flag'=>1,
						'message'=>'编辑成功',
						'link'=>'返回列表',
						'href'=>site_url('admin/news')
					);
     			}
     			else{
     				$data['data']=array(
						'flag'=>0,
						'message'=>'编辑失败',
						'link'=>'返回',
						'href'=>site_url('admin/news')
					);
     			}
     			$this->load->view('admin/show_info',$data);
     			
			}
		}
		function delete(){
			$id = floor(floatval($this->uri->segment(4)));
			if(!$id){
				$this->index();
				return;
			}
			$return=$this->db->where('id',$id)->delete('news');
			if($return){
 				$data['data']=array(
					'flag'=>1,
					'message'=>'删除成功',
					'href'=>site_url('admin/news')
				);
 			}
 			else{
 				$data['data']=array(
					'flag'=>0,
					'message'=>'编辑失败',
					'href'=>site_url('admin/news')
				);
 			}
 			$this->load->view('admin/show_info',$data);
 		}
		
	}
	
?>